package models;

import java.util.*;
import javax.persistence.*;
import play.data.binding.As;
import play.data.validation.*;
import play.db.jpa.*;

@Entity
@Table(name = "articles")
public class Article extends Model {

    @Required
    public String title;
    @As("dd/MM/yyyy")
    public Date created;
    @As("dd/MM/yyyy")
    public Date updated;
    @Lob
    @Required
    public String content;
    public String images;
    public String resume;
    @ManyToOne
    public User user;
    @ManyToOne
    @Required
    public Category category;

    public Article(
            String title,
            @As("dd/MM/yyyy") Date created,
            @As("dd/MM/yyyy") Date updated,
            String content,
            String images,
            String resume,
            User user,
            Category category) {
        this.title = title;
        this.created = created;
        this.updated = updated;
        this.content = content;
        this.images = images;
        this.user = user;
        this.category = category;

    }

    @Override
    public String toString() {
        return this.title;
    }

    public String formatTitle(int l) {
        if (this.title.length() > l) {
            return this.title.substring(0, l) + "...";
        } else {
            return this.title;
        }
    }

    public String formatResume() {
        if (this.content.length() <= 117) {
            return this.content + "...";
        } else {
            return this.resume = this.content.substring(0, 117) + "...";
        }


    }
}